var Config = require("Config");
var saveManager = require("saveManager");

cc.Class({
    extends: cc.Component,

    properties: {
		
		narutoPrefab:{
			default:null,
			type:cc.Prefab
		},
		
		audioSword: {
            type: cc.AudioClip,
            default: null
        },
		
		audioBoom: {
            type: cc.AudioClip,
            default: null
        },
		
		audioStart: {
            type: cc.AudioClip,
            default: null
        },
		
		audioStop: {
            type: cc.AudioClip,
            default: null
        },
		audioDanger: {
            type: cc.AudioClip,
            default: null
        },
		audioDoor: {
            type: cc.AudioClip,
            default: null
        },
		audioUpdown: {
            type: cc.AudioClip,
            default: null
        },
		audioItem: {
            type: cc.AudioClip,
            default: null
        },
		audioDisappear: {
            type: cc.AudioClip,
            default: null
        },
		audioDie: {
            type: cc.AudioClip,
            default: null
        },
		player: {
            default: null,
            type: cc.Node
        },
		playerFrameL: {
            default: null,
            type: cc.SpriteFrame
        },
		playerFrameR: {
            default: null,
            type: cc.SpriteFrame
        },
		playerFrameU: {
            default: null,
            type: cc.SpriteFrame
        },
		playerFrameD: {
            default: null,
            type: cc.SpriteFrame
        },
		gameMask: {
            default: null,
            type: cc.Node
        },
		buttonClose: {
            default: null,
            type: cc.Node
        },
		buttonu: {
            default: null,
            type: cc.Node
        },
		buttond: {
            default: null,
            type: cc.Node
        },
		buttonl: {
            default: null,
            type: cc.Node
        },
		buttonr: {
            default: null,
            type: cc.Node
        },
		buttona: {
            default: null,
            type: cc.Node
        },
		buttonb: {
            default: null,
            type: cc.Node
        },
		
		buttonSave: {
            default: null,
            type: cc.Node
        },
		
		buttonSave0: {
            default: null,
            type: cc.Node
        },
		
		buttonSave1: {
            default: null,
            type: cc.Node
        },
		
		buttonSave2: {
            default: null,
            type: cc.Node
        },
		
		SaveSelect: {
            default: null,
            type: cc.Node
        },
		
		buttonSaveGet: {
            default: null,
            type: cc.Node
        },
		
		buttonSaveClose: {
            default: null,
            type: cc.Node
        },
		
		npcPrefab:{
			default:null,
			type:cc.Prefab
		},
		lightPrefab:{
			default:null,
			type:cc.Prefab
		},
		npcHelperPrefab:{
			default:null,
			type:cc.Prefab
		},
		keyPrefab:{
			default:null,
			type:cc.Prefab
		},
		chapterButtonPrefab:{
			default:null,
			type:cc.Prefab
		},
		keyYPrefab:{
			default:null,
			type:cc.Prefab
		},
		keyRPrefab:{
			default:null,
			type:cc.Prefab
		},
		keyGPrefab:{
			default:null,
			type:cc.Prefab
		},
		swordPrefab:{
			default:null,
			type:cc.Prefab
		},
		dunPrefab:{
			default:null,
			type:cc.Prefab
		},
		sword3Prefab:{
			default:null,
			type:cc.Prefab
		},
		dun3Prefab:{
			default:null,
			type:cc.Prefab
		},
		sword5Prefab:{
			default:null,
			type:cc.Prefab
		},
		dun5Prefab:{
			default:null,
			type:cc.Prefab
		},
		
		flytargetPrefab:{
			default:null,
			type:cc.Prefab
		},
		
		icePrefab:{
			default:null,
			type:cc.Prefab
		},
		tenPrefab:{
			default:null,
			type:cc.Prefab
		},
		godPrefab:{
			default:null,
			type:cc.Prefab
		},
		
		bottlePrefab:{
			default:null,
			type:cc.Prefab
		},
		
		bottle2Prefab:{
			default:null,
			type:cc.Prefab
		},
		doorPrefab:{
			default:null,
			type:cc.Prefab
		},
		doorYPrefab:{
			default:null,
			type:cc.Prefab
		},
		doorGPrefab:{
			default:null,
			type:cc.Prefab
		},
		rectPrefab:{
			default:null,
			type:cc.Prefab
		},
		rectPrefab2:{
			default:null,
			type:cc.Prefab
		},
		downtPrefab:{
			default:null,
			type:cc.Prefab
		},
		uptPrefab:{
			default:null,
			type:cc.Prefab
		},
		dimonAPrefab:{
			default:null,
			type:cc.Prefab
		},
		dimonDPrefab:{
			default:null,
			type:cc.Prefab
		},
		goldPrefab:{
			default:null,
			type:cc.Prefab
		},
		boomPrefab:{
			default:null,
			type:cc.Prefab
		},
		
		flyPrefab:{
			default:null,
			type:cc.Prefab
		},
		flyTPrefab:{
			default:null,
			type:cc.Prefab
		},
		flyUPrefab:{
			default:null,
			type:cc.Prefab
		},
		flyDPrefab:{
			default:null,
			type:cc.Prefab
		},
		boomItemPrefab:{
			default:null,
			type:cc.Prefab
		},
		
		hpDisplay: {
            default: null,
            type: cc.Label
        },
		attackDisplay: {
            default: null,
            type: cc.Label
        },
		defendDisplay: {
            default: null,
            type: cc.Label
        },
		keyDisplay: {
            default: null,
            type: cc.Label
        },
		keyYDisplay: {
            default: null,
            type: cc.Label
        },
		keyRDisplay: {
            default: null,
            type: cc.Label
        },
		chapterDisplay: {
            default: null,
            type: cc.Label
        },
		iceDisplay: {
            default: null,
            type: cc.Node
        },
		tenDisplay: {
            default: null,
            type: cc.Node
        },
		godDisplay: {
            default: null,
            type: cc.Node
        },
		flyDisplay: {
            default: null,
            type: cc.Node
        },
		flyTDisplay: {
            default: null,
            type: cc.Node
        },
		flyDDisplay: {
            default: null,
            type: cc.Node
        },
		flyUDisplay: {
            default: null,
            type: cc.Node
        },
		boomDisplay: {
            default: null,
            type: cc.Node
        },
		fileNode:{
			default: null,
            type: cc.Node
		},
		goldDisplay:{
			default: null,
            type: cc.Label
		}

    },
	
    loadMap: function () {
		
		
		if(this.chapterNmb == 11&&!this.upload1){
			this.upload1 = true;
			console.log("上传第一关成绩");
			this.uploadWxScore(1);
		}
		
		if(this.chapterNmb == 21&&!this.upload2){
			this.upload2 = true;
			console.log("上传第二关成绩");
			this.uploadWxScore(2);
		}
		
		
		this.tiledMap = this.node.getComponent(cc.TiledMap);
		
		var self = this;
		
		this.fileNode.active = false;
		
		this.chapterButtonArr[this.chapterNmb].getComponent(cc.Button).interactable = true;
        
		cc.loader.loadRes(this.Config.chapterConfig[this.chapterNmb].map, function(err, map){
			
			self.tiledMap.tmxAsset = map;
			
			self.wall = self.tiledMap.getLayer('wall');
			
			if(self.tiledMap.getLayer('fire'))
				self.firewall = self.tiledMap.getLayer('fire');
			else
				self.firewall = null;
			
			var posx = self.playerPx - 25,posy = self.playerPy-25;
			
			self.node.setPosition(cc.v2(-posx*32,posy*32));
			
			self.node.runAction(cc.sequence(cc.moveBy(0.1,0,1),cc.moveBy(0.1,0,-1)));
		});
    },
	
	getGamePos: function(x,y){
		return cc.v2(this.centerx+(x-this.centerpx)*this.cellSize,this.centery-(y-this.centerpy)*this.cellSize);
	},

    onLoad: function () {
		
		if(cc.sys.platform === cc.sys.WECHAT_GAME){
			wx.showShareMenu();
		}
		
		cc.audioEngine.play(this.audioStart, false, 1);
		
		this.saveManagerInstance = new saveManager();
		
		this.Config = new Config(this);
		
		//非动态参数
		this.moveOnceTime = 0.1;
		this.rectNode = null;
		this.rectNode2 = null;
		this.battleing = 0;//是否在战斗中
		this.moving = 0;//是否在移动中
		this.beatMonsterIndex = -1;//选中npc的下标
		this.maxH = 50;
		this.maxW = 50;
		this.cellSize = 32;
		this.dir = 0;
		this.centerx = 16;//地图正中间对应的屏幕x坐标
		this.centery = -16;//地图正中间对应的屏幕y坐标
		this.centerpx = 25;//地图正中间x坐标
		this.centerpy = 25;//地图正中间y坐标
		this.playName = "none";
		
		//动态参数
		this.chapterNmb = 0;//当前楼层
		this.keyNmb = 0;//钥匙数量
		this.keyNmbY = 0;//黄钥匙数量
		this.keyNmbR = 0;//红钥匙数量
		this.attack = 10;//攻击力
		this.defends = 10;//防御力
		this.hp = 1000;//血量
		this.iceAble = 0;//千年冰
		this.tenAble = 0;//十字架
		this.godAble = 0;//降魔刀
		this.goldNmb = 0;//金币数
		this.flyTNmb = 0;//对称飞机
		this.flyAble = 0;//飞机
		this.flyUNmb = 0;//向上飞机
		this.flyDNmb = 0;//向下飞机
		this.boomNmb = 0;//炸弹数
		this.price = 20;//当前购买价格
		this.playerPx = 25;//玩家所处地图中的x坐标
		this.playerPy = 25;//玩家所处地图中的y坐标
		this.upload1 = false;//是否到达11
		this.upload2 = false;//是否到达21
		this.upload3 = false;//是否通关
		
		
		this.moveconf = this.Config.moveConf;
		this.monstorconf = this.Config.monstorConf;
		
		this.initButton();
		this.initAll();
		this.initCallBack();
		
		this.initAd();
    },
	
	initAd:function(){
		if(cc.sys.platform === cc.sys.WECHAT_GAME){
			
		/*
		this.bannerAd = wx.createBannerAd({
			adUnitId: 'adunit-e447cee5d2432576',
			style: {
				left: 0,
				top: 0,
				width: 350
			}
		});
		*/
		let self = this;
		
		wx.getSystemInfo({
			success (res) {
				console.log(res.model)
				console.log(res.pixelRatio)
				console.log(res.windowWidth)
				console.log(res.windowHeight)
				console.log(res.language)
				console.log(res.version)
				console.log(res.platform)
				
				
				self.bannerAd  = wx.createBannerAd({
					adUnitId: 'adunit-e447cee5d2432576',
					adIntervals:'30',
					style: {
						left: (res.windowWidth-300)/2,
						top: 0,
						width: 300,
						height:100
					}
				})
				
				self.bannerAd.onError(err => {
					console.log(err)
				})
				
				
				self.bannerAd.show();
				
			}
		})
		
		
		
		

		}	
		
	},
	
	showAd:function(){
		// 在适合的场景显示 Banner 广告
		if(cc.sys.platform === cc.sys.WECHAT_GAME&&this.bannerAd){
			//this.bannerAd.show();
		}
	},
	hideAd:function(){
		// 在适合的场景显示 Banner 广告
		if(cc.sys.platform === cc.sys.WECHAT_GAME&&this.bannerAd){
			//this.bannerAd.hide();
		}
	},
	
	uploadWxScore:function(level){
		
		if(cc.sys.platform === cc.sys.WECHAT_GAME){
			
			console.log("上传微信成绩");
			
			wx.cloud.init({
			// 此处请填入环境 ID, 环境 ID 可打开云控制台查看
			env: 'magict-kwdcy',
			traceUser: true,
			})
			
			var testDate = new Date();
		
		var mytime=testDate.toLocaleString();
			
			 var db = wx.cloud.database();
			db.collection('score').add({data: {
				hp:this.hp,
				attack:this.attack,
				defends:this.defends,
				key1:this.keyNmbY,
				key2:this.keyNmb,
				key3:this.keyNmbR,
				gold:this.goldNmb,
				price:this.price,
				level:level,
				time:mytime
			}})
				.then(res => {
					console.log(res)
				})
					.catch(console.error);
				}
	
	},
	
	initCallBack:function(){
		
		this.beatNpcCallback = cc.callFunc(function () {
				this.beatOneMonstor();
		}, this); 
		 
		this.beatPlayerCallback = cc.callFunc(function () {
				this.beatPlayer();
		}, this);
		   
		this.mapMoveCallBack = cc.callFunc(function () {
			if(this.getItem(this.playerPx,this.playerPy)){
				this.mapMove();
			}else{
				this.moving = 0;
			}
		}, this);

	},
	
	initAll:function(){
		
		var allarr = [this.npcArr,this.doorArr,this.itemArr,this.upAndDownArr,this.npcHelperArr,this.narutoArr];
		
		for(var i=0;i<allarr.length;i++){
			if(allarr[i]){
				for(var j=0;j<allarr[i].length;j++){
					this.node.removeChild(allarr[i][j].node);
				}
			}
		}
		
		if(this.flytargetNode){//传送点
			this.node.removeChild(this.flytargetNode);
		}
		var flytargetpos = this.Config.chapterConfig[this.chapterNmb].flytarget;
		if(flytargetpos){
			this.createFlytarget(flytargetpos.x,flytargetpos.y);
		}
		
		
		this.initNpcHelper();
		
		this.initDoor();
		
		this.initItem();
		
		this.initNpc();
		
		this.initUpAndDown();
		
		this.initNaruto();
		
		this.loadMap();
		
		this.gameMask.active = false;
		this.SaveSelect.active = false;
		
		
		if(this.godAble<=0){
			this.godDisplay.active = false;
		}else{
			this.godDisplay.active = true;
		}
		
		if(this.tenAble<=0){
			this.tenDisplay.active = false;
		}else{
			this.tenDisplay.active = true;
		}
		
		if(this.iceAble<=0){
			this.iceDisplay.active = false;
		}else{
			this.iceDisplay.active = true;
		}
		
		if(this.flyAble<=0)
			this.flyDisplay.active = false;
		else
			this.flyDisplay.active = true;
		
		if(this.flyTNmb<=0)
			this.flyTDisplay.active = false;
		else
			this.flyTDisplay.active = true;
		
		if(this.flyUNmb<=0)
			this.flyUDisplay.active = false;
		else
			this.flyUDisplay.active = true;
		
		if(this.flyDNmb<=0)
			this.flyDDisplay.active = false;
		else
			this.flyDDisplay.active = true;
		
		if(this.boomNmb<=0)
			this.boomDisplay.active = false;
		else
			this.boomDisplay.active = true;
		
	},
	
	initNaruto:function(){
		this.narutoArr = [];
		var tempconf = this.Config.chapterConfig[this.chapterNmb].naruto;
		if(tempconf){
			for(var i=0;i<tempconf.length;i++){
				this.createNaruto(tempconf[i].x,tempconf[i].y,tempconf[i].target,tempconf[i].targetx,tempconf[i].targety);
			}
		}
	},
	
	initUpAndDown:function(){
		this.upAndDownArr = [];
		var tempconf = this.Config.chapterConfig[this.chapterNmb].upAndDown;
		for(var i=0;i<tempconf.length;i++){
			this.createUpAndDown(tempconf[i].x,tempconf[i].y,tempconf[i].dir);
		}
	},
	
	initItem:function(){
		this.itemArr = [];
		var tempconf = this.Config.chapterConfig[this.chapterNmb].item;
		for(var i=0;i<tempconf.length;i++){
			this.createItem(tempconf[i].x,tempconf[i].y,tempconf[i].t,tempconf[i].value);
		}
	},
	
	initNpc:function(){
		this.npcArr = [];
		var tempconf = this.Config.chapterConfig[this.chapterNmb].npc;
		for(var i=0;i<tempconf.length;i++){
			this.createNpc(tempconf[i].x,tempconf[i].y,tempconf[i].t);
		}
	},
	
	initNpcHelper:function(){
		this.npcHelperArr = [];
		var tempconf = this.Config.chapterConfig[this.chapterNmb].npcHelper;
		if(tempconf)
		for(var i=0;i<tempconf.length;i++){
			this.createNpcHelper(tempconf[i].x,tempconf[i].y,tempconf[i].t,tempconf[i].message,tempconf[i].t2,tempconf[i].liveAble,
				tempconf[i].buyKey,tempconf[i].buyValue);
		}
	},
	
	initDoor:function(){
		this.doorArr = [];
		var tempconf = this.Config.chapterConfig[this.chapterNmb].door;
		for(var i=0;i<tempconf.length;i++){
			this.createDoor(tempconf[i].x,tempconf[i].y,tempconf[i].t);
		}
	},
	
	refreshSaveSelect:function(){
			
		if(this.saveManagerInstance.getSavedTime(1)){
			this.buttonSave0.children[0].children[0].getComponent(cc.Label).string = "存档1 "+this.saveManagerInstance.getSavedTime(1);
		}else{
			this.buttonSave0.children[0].children[0].getComponent(cc.Label).string = "存档1 空白";
		}
		
		if(this.saveManagerInstance.getSavedTime(2)){
			this.buttonSave1.children[0].children[0].getComponent(cc.Label).string = "存档2 "+this.saveManagerInstance.getSavedTime(2);
		}else{
			this.buttonSave1.children[0].children[0].getComponent(cc.Label).string = "存档2 空白";
		}
		
		if(this.saveManagerInstance.getSavedTime(3)){
			this.buttonSave2.children[0].children[0].getComponent(cc.Label).string = "存档3 "+this.saveManagerInstance.getSavedTime(3);
		}else{
			this.buttonSave2.children[0].children[0].getComponent(cc.Label).string = "存档3 空白";
		}
			
	},
	
	initButton:function(){
		
		this.buttonSave0.on(cc.Node.EventType.TOUCH_END, function(){
			
			if(this.saveType == 1){
				console.log("覆盖存档1");
				
				if(this.saveManagerInstance.saveGame(this.Config,this,1)){
					this.SaveSelect.active = false;
				}
			}else if(this.saveType == 2){
				console.log("读取存档1");
				if(this.saveManagerInstance.goonGame(this.Config,this,1)){
					this.SaveSelect.active = false;
				}
			}
			
		}, this);
		
		this.buttonSave1.on(cc.Node.EventType.TOUCH_END, function(){
			
			if(this.saveType == 1){
				console.log("覆盖存档2");
				if(this.saveManagerInstance.saveGame(this.Config,this,2)){
					this.SaveSelect.active = false;
				}
			}else if(this.saveType == 2){
				console.log("读取存档2");
				if(this.saveManagerInstance.goonGame(this.Config,this,2)){
					this.SaveSelect.active = false;
				}
			}
			
		}, this);
		
		this.buttonSave2.on(cc.Node.EventType.TOUCH_END, function(){
			
			if(this.saveType == 1){
				console.log("覆盖存档3");
				if(this.saveManagerInstance.saveGame(this.Config,this,3)){
					this.SaveSelect.active = false;
				}
			}else if(this.saveType == 2){
				console.log("读取存档3");
				if(this.saveManagerInstance.goonGame(this.Config,this,3)){
					this.SaveSelect.active = false;
				}
			}
			
		}, this);
		
		this.buttonSave.on(cc.Node.EventType.TOUCH_END, function(){
			
			if(!this.canUseItem())return;
			
			this.SaveSelect.active = true;
			this.saveType = 1;
			this.refreshSaveSelect();
			
		}, this);
		
		this.buttonSaveGet.on(cc.Node.EventType.TOUCH_END, function(){
			
			if(!this.canUseItem())return;
			
			this.SaveSelect.active = true;
			this.saveType = 2;
			this.refreshSaveSelect();
		}, this);
		
		this.buttonSaveClose.on(cc.Node.EventType.TOUCH_END, function(){
			this.SaveSelect.active = false;
			this.saveType = 0;
		}, this)
		
		this.buttond.on(cc.Node.EventType.TOUCH_START, function(){
			this.onButtonDownDir(40);
		}, this); 
		this.buttond.on(cc.Node.EventType.TOUCH_END, this.onMouseUp, this); 
		
		this.buttonr.on(cc.Node.EventType.TOUCH_START, function(){
			this.onButtonDownDir(39);
		}, this); 
		this.buttonr.on(cc.Node.EventType.TOUCH_END, this.onMouseUp, this); 
		
		this.buttonl.on(cc.Node.EventType.TOUCH_START, function(){
			this.onButtonDownDir(37);
		}, this); 
		this.buttonl.on(cc.Node.EventType.TOUCH_END, this.onMouseUp, this); 
		
		this.buttonu.on(cc.Node.EventType.TOUCH_START, function(){
			this.onButtonDownDir(38);
		}, this); 
		this.buttonu.on(cc.Node.EventType.TOUCH_END, this.onMouseUp, this); 
		
		this.buttona.on(cc.Node.EventType.TOUCH_START, this.onButtonA, this);
		this.buttonb.on(cc.Node.EventType.TOUCH_START, this.onButtonB, this);
		
		this.flyDisplay.on(cc.Node.EventType.TOUCH_END, function(){
			if(this.canUseItem()&&this.flyAble>0)
				this.gameMask.active = true;
		}, this);
		
		this.flyTDisplay.on(cc.Node.EventType.TOUCH_END, function(){
			console.log("飞对称");
			if(this.canUseItem()&&this.flyTNmb>0){
				if(this.flyToPos()){
					this.flyTNmb--;
				}
			}
		}, this);
		
		this.flyUDisplay.on(cc.Node.EventType.TOUCH_END, function(){
			console.log("向上飞");
			if(this.canUseItem()&&this.flyUNmb>0){
				if(this.flyToChapter(this.chapterNmb+1)){
					this.flyUNmb--;
				}
			}
		}, this);
		
		this.flyDDisplay.on(cc.Node.EventType.TOUCH_END, function(){
			console.log("向下飞");
			if(this.canUseItem()&&this.flyDNmb>0){
				if(this.flyToChapter(this.chapterNmb-1)){
					this.flyDNmb--;
				}
			}
		}, this);
		
		this.boomDisplay.on(cc.Node.EventType.TOUCH_END, function(){
			console.log("爆炸");
			if(this.canUseItem()&&this.boomNmb>0){
				
				if(this.boomMonster()){
					this.boomNmb--;
				}
				
			}
		}, this);
		
		this.buttonClose.on(cc.Node.EventType.TOUCH_END, function(){
			this.gameMask.active = false;
		}, this);
		
		this.initChapterButton();
		
	},
	
	canUseItem:function(){
		return this.moving!=1&&this.rectNode==null&&this.rectNode2==null;
	},
	
	initChapterButton:function(){
		
		var btnstartx = -210,btnstarty = 180;
		this.chapterButtonArr = [];
		for(var i=0;i<this.Config.chapterConfig.length;i++){
			
			var btn = cc.instantiate(this.chapterButtonPrefab);
			
			btn.children[0].children[0].getComponent(cc.Label).string = i;
			
			this.gameMask.addChild(btn);
			
			if(i==this.chapterNmb){
				btn.getComponent(cc.Button).interactable = true;
			}else{
				btn.getComponent(cc.Button).interactable = false;
			}
			
			if(i%7==0){
				btnstartx = -210;
				btnstarty-=60;
			}else{
				btnstartx += 70;
			}
			
			btn.setPosition(cc.v2(btnstartx,btnstarty));
			
			let btnindex = i;
			btn.on(cc.Node.EventType.TOUCH_END, function(){
				console.log(btnindex);
				if(this.chapterButtonArr[btnindex].getComponent(cc.Button).interactable){
					this.gameMask.active = false;
					this.flyToChapter(btnindex);
				}
			}, this);
			
			this.chapterButtonArr.push(btn);
		}
		
	},
	
	showBoom:function(tx,ty){
		
		var boomnode = this.createNode(tx,ty,this.boomPrefab);
		boomnode.runAction(cc.sequence(cc.delayTime(0.5),cc.callFunc(function(){
				this.getParent().removeChild(this);
		},boomnode)));
		
	},
	
	boomMonster:function(){
		var x = this.playerPx,y = this.playerPy;
		
		var posarr = [[x,y+1],[x,y-1],[x+1,y],[x-1,y]];
		
		var nmb = 0;
		
		for(var i = 0 ;i<posarr.length;i++){
			
			var tx = posarr[i][0],ty = posarr[i][1];
			
			for(var j=0;j<this.npcArr.length;j++){
				var npc = this.npcArr[j];
				
				if(!npc.isboss){//boss无效
				
					var npcposarr =[[npc.x,npc.y]];
				
					if(npc.extra){
					for(var k = 0;k<npc.extra.length;k++){
						npcposarr.push([npc.x+npc.extra[k][0],npc.y+npc.extra[k][1]]);
					}
					}
				
					for(var k=0;k<npcposarr.length;k++){
					if(npcposarr[k][0] == tx&&npcposarr[k][1] == ty){
					
					if(npc.gold){//npc掉落金币
				
						var npcx = npc.x,npcy=npc.y;
				
						this.Config.chapterConfig[this.chapterNmb].item.push({x:npcx,y:npcy,t:90,value:npc.gold});
				
						this.createItem(npcx,npcy,90,npc.gold);
				
					}
					
					this.showBoom(tx,ty);
					
					this.npcArr.splice(j,1);
			
					this.Config.chapterConfig[this.chapterNmb].npc.splice(j,1);
			
					this.node.removeChild(npc.node);
					
					nmb++;
					
					break;
					
					}
					}
				}
			}
			
		}
		
		if(nmb>0){
			
			cc.audioEngine.play(this.audioBoom, false, 1);
			
			return true;
			
		}else{
			return false;
		}
		
	},
	
	flyToPos:function(){
		
		var tempPx=-1,tempPy=-1;
		
		if(this.playerPx<25){
			tempPx = 25 + (25-this.playerPx);
		}else{
			tempPx = 25 - (this.playerPx-25);
		}
		
		if(this.playerPy<=25){
			tempPy = 25+(25-this.playerPy);
		}else {
			tempPy = 25 - (this.playerPy - 25);
		}
		
		console.log(this.playerPx,this.playerPy,tempPx,tempPy);
		
		if(this.canMove(tempPx,tempPy,1)&&this.noNode(tempPx,tempPy)){
		
			this.playerPx = tempPx;
			this.playerPy = tempPy;
		
			this.initAll();
			
			this.showLight();
			
			return true;
		}else{
			cc.audioEngine.play(this.audioDanger, false, 1);
			
			return false;
		}
		
	},
	
	flyToPosAndChapter:function(x,y,chapter){
		
		this.playerPx = x;
			
		this.playerPy = y;
			
		this.chapterNmb=chapter;
				
		this.dir = 0;
				
		this.resetAnimation();
				
		this.initAll();
			
		this.showLight();
	},
	
	noNode:function(x,y){
		
		var allarr = [this.npcArr,this.doorArr,this.itemArr,this.upAndDownArr,this.npcHelperArr];
		
		for(var i=0;i<allarr.length;i++){
			if(allarr[i]){
				for(var j=0;j<allarr[i].length;j++){
					if(x == allarr[i][j].x && y == allarr[i][j].y)
						return false;
				}
			}
		}
		
		return true;
	},
	
	showLight:function(){
		var light = cc.instantiate(this.lightPrefab);
		this.player.addChild(light);
		light.runAction(cc.sequence(cc.delayTime(0.5),cc.callFunc(function(){
				this.getParent().removeChild(this);
		},light)));
					
		cc.audioEngine.play(this.audioUpdown, false, 1);
	},
	
	flyToChapter:function(level){
		
		if(this.Config.chapterConfig[level] && this.Config.chapterConfig[level].flytarget){
		
			this.playerPx = this.Config.chapterConfig[level].flytarget.x;
			
			this.playerPy = this.Config.chapterConfig[level].flytarget.y;
			
			this.chapterNmb=level;
				
			this.dir = 0;
				
			this.resetAnimation();
				
			this.initAll();
			
			this.showLight();
			
			return true;
			
		}else{
			
			cc.audioEngine.play(this.audioDanger, false, 1);
			console.log("楼层不存在");
			
			return false;
		}
		
	},
	
	createNode:function(x,y,prefab){
		var gamepos = this.getGamePos(x,y);
		var node = cc.instantiate(prefab);
		this.node.addChild(node);
		node.setPosition(gamepos.x,gamepos.y);
		return node;
	},
	
	createItem:function(x,y,itemType,value){
		
		var itemconf = this.Config.itemConf[itemType]
		
		var prefab = this[itemconf.prefab];
		
		this.itemArr.push({node:this.createNode(x,y,prefab),x:x,y:y,t:itemType,value:value})
		
		
	},
	
	createFlytarget:function(x,y){
		
		console.log("flytarget",x,y);
		
		this.flytargetNode = this.createNode(x,y,this.flytargetPrefab);
	},
	
	createUpAndDown:function(x,y,dir){
		
		var prefab = null;
		
		if(dir>0){
			prefab = this.uptPrefab;
		}else if(dir<0){
			prefab = this.downtPrefab;
		}
		
		if(prefab!=null)
		this.upAndDownArr.push({node:this.createNode(x,y,prefab),x:x,y:y,dir:dir});
	},
	
	createNaruto:function(x,y,target,tx,ty){
		this.narutoArr.push({node:this.createNode(x,y,this.narutoPrefab),x:x,y:y,target:target,targetx:tx,targety:ty});
	},
		
	createDoor:function(x,y,t){
		
		var doorconf = this.Config.doorConf[t];
		
		this.doorArr.push({node:this.createNode(x,y,this[doorconf.prefab]),x:x,y:y,t:t});
	},
		
	createNpc:function(x,y,type){
		
		var conf = this.monstorconf[type];
		
		var node = this.createNode(x,y,this.npcPrefab);
		
		node.getComponent(cc.Animation).play(conf.ani);
		
		this.npcArr.push({isboss:conf.isboss,gold:conf.gold,node:node,weak:conf.weak,ani:conf.ani,name:conf.name,skill:conf.skill,x:x,y:y,mhp:conf.mhp,mac:conf.mac,mde:conf.mde,extra:conf.extra,scale:conf.scale});
	},
	
	createNpcHelper:function(x,y,type,message,type2,liveAble,buyKey,buyValue){
		var conf = this.Config.helperConf[type];
		
		var node = this.createNode(x,y,this.npcHelperPrefab);
		
		node.getComponent(cc.Animation).play(conf.ani);
		
		this.npcHelperArr.push({node:node,ani:conf.ani,name:conf.name,message:message,x:x,y:y,type2:type2,liveAble:liveAble,buyKey:buyKey,buyValue:buyValue});
	},
	
	update(dt){
		
		this.hpDisplay.string = ":"+this.hp;
		this.attackDisplay.string=":"+this.attack;
		this.defendDisplay.string=":"+this.defends;
		this.keyDisplay.string=":"+this.keyNmb;
		this.keyYDisplay.string=":"+this.keyNmbY;
		this.keyRDisplay.string=":"+this.keyNmbR;
		this.goldDisplay.string=":"+this.goldNmb;
		this.chapterDisplay.string="第"+this.chapterNmb+"层";
		
		this.flyTDisplay.children[1].getComponent(cc.Label).string = this.flyTNmb;
		this.flyDDisplay.children[1].getComponent(cc.Label).string = this.flyDNmb;
		this.flyUDisplay.children[1].getComponent(cc.Label).string = this.flyUNmb;
		this.boomDisplay.children[1].getComponent(cc.Label).string = this.boomNmb;
		
		/*
		if(this.iceAble>0){
			this.iceDisplay.active = true;
		}else{
			this.iceDisplay.active = false;
		}
		
		if(this.tenAble>0){
			this.tenDisplay.active = true;
		}else{
			this.tenDisplay.active = false;
		}
		
		if(this.godAble>0){
			this.godDisplay.active = true;
		}else{
			this.godDisplay.active = false;
		}*/
	},
	
	//npc战斗弹窗
	showRect:function(i){
		
		if(this.rectNode==null){
			
			cc.audioEngine.play(this.audioDanger, false, 1);
			
			this.resetAnimation();
			
			this.battleing = 1
					
			this.dir = 0;
					
			this.beatMonsterIndex = i;
		
			var node = cc.instantiate(this.rectPrefab);
			this.player.addChild(node);
			node.setPosition(0,0);
			
			var npc = this.npcArr[this.beatMonsterIndex];
			
			node.getChildByName('face').getComponent(cc.Animation).play(npc.ani);
			if(npc.scale){
				node.getChildByName('face').setScale(npc.scale);
			}
			node.getChildByName('name').getComponent(cc.Label).string = "名称："+npc.name;
			node.getChildByName('hp').getComponent(cc.Label).string = "生命："+npc.mhp;
			//node.getChildByName('skill').getComponent(cc.Label).string = "技能："+npc.skill;
			node.getChildByName('gold').getComponent(cc.Label).string = "掉落："+(npc.gold?npc.gold:0);
			
			var tempac = npc.mac,tempde = npc.mde;
			if(npc.weak&&this[npc.weak]){
				//tempac = Math.floor(npc.mac/2);
				tempde = 0;
			}

			node.getChildByName('attack').getComponent(cc.Label).string = "攻击："+tempac;
			node.getChildByName('defends').getComponent(cc.Label).string = "防御："+tempde;
			node.getChildByName('cuthp').getComponent(cc.Label).string = "预计掉血："+this.calCutHp(npc.mhp,tempac,tempde);
	
			this.rectNode = node;
			
			this.showAd();
		}
		
	},
	
	//计算伤害
	calCutHp:function(npchp,npcmac,npcmde){
		
		if(this.attack<=npcmde){
			return 99999;
		}else{
			if(npcmac>this.defends){
				return (Math.ceil(npchp/(this.attack-npcmde))-1)*(npcmac-this.defends);
			}else{
				return 0;
			}
		}
		
	},
	
	//npc对话弹窗
	showRect2:function(i){
		
		if(this.rectNode2==null){
			cc.audioEngine.play(this.audioStop, false, 1);
			
			this.resetAnimation();
			
			this.battleing = 1
					
			this.dir = 0;
					
			this.beatMonsterIndex = i;
		
			var node = cc.instantiate(this.rectPrefab2);
			this.player.addChild(node);
			node.setPosition(0,0);
			
			var npc = this.npcHelperArr[this.beatMonsterIndex];
			
			node.getChildByName('face').getComponent(cc.Animation).play(npc.ani);
			
			node.getChildByName('message').getComponent(cc.Label).string = npc.message.replace('$', this.price);
			
			if(npc.type2 == 3){
				
				node.getChildByName('A').getComponent(cc.Label).string = "A：我买我买";
				node.getChildByName('B').getComponent(cc.Label).string = "B：没钱,滚";
			}
	
			this.rectNode2 = node;
			
			this.showAd();
		}
		
	},
	
	onButtonA:function(){
		
		if(this.rectNode!=null){
			this.closeRect();
			this.beatOneMonstor();
		}else if(this.rectNode2!=null){
			
			var npc = this.npcHelperArr[this.beatMonsterIndex];
			
			var npcNode = npc.node;
			
			if(!npc.liveAble){
			
				cc.audioEngine.play(this.audioDisappear, false, 1);
			
				npcNode.runAction(cc.sequence(cc.fadeOut(1),cc.callFunc(function(){
					this.getParent().removeChild(this);
				},npcNode)));
			
				this.npcHelperArr.splice(this.beatMonsterIndex,1);
				
				this.Config.chapterConfig[this.chapterNmb].npcHelper.splice(this.beatMonsterIndex,1);
			}
			
			if(npc.type2==1){//游戏结束
			
				//上传第三关成绩
				
				if(cc.sys.platform === cc.sys.WECHAT_GAME){
					this.uploadWxScore(3);
				}
				
				cc.director.loadScene("happyending");
				
			}else if(npc.type2==2){//分享npc
				
				console.log("分享游戏");
				
				
				if(cc.sys.platform === cc.sys.WECHAT_GAME){
				
					
				let self = this;
				
				
				if(!this.rewardedVideoAd){
					
					this.rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: 'adunit-e52efa2a0c54bfab' });
					
					this.rewardedVideoAd.onError(err => {
						console.log(err)
						/*
						//视频加载识别改为分享
						wx.shareAppMessage({
						title: '硬核魔塔经典回归,快来挑战'
						});
					
						self.node.runAction(cc.sequence(cc.delayTime(1),cc.callFunc(function(){
						self.keyNmbY+=3;
						},this)));*/
						self.closeRect2();
						
					});
					
					
					this.rewardedVideoAd.onClose(res => {
						// 用户点击了【关闭广告】按钮
						// 小于 2.1.0 的基础库版本，res 是一个 undefined
						if (res && res.isEnded || res === undefined) {
						// 正常播放结束，可以下发游戏奖励
							self.keyNmbY+=3;
							self.closeRect2();
						}
						else {
						// 播放中途退出，不下发游戏奖励
						}
					})
					
					//
				}
				
				
				this.rewardedVideoAd.show()
				.catch(err => {
					 self.rewardedVideoAd.load().
					 then(() => self.rewardedVideoAd.show())
				})
				
				
				}else{
					//非微信环境
					this.closeRect2();
				}
				
			}else if(npc.type2==3){//商店npc
			
				if(this.goldNmb>=this.price){
					
					cc.audioEngine.play(this.audioStop, false, 1);
					
					this.goldNmb -= this.price;
					
					this.price*=2;
					
					this.closeRect2();
					
					if(npc.buyKey&&npc.buyValue){
						this[npc.buyKey]+=npc.buyValue/1;
					}
					
				}else{
					
					cc.audioEngine.play(this.audioDanger, false, 1);
				}
				
			}else{
				this.closeRect2();
			}
			
			
		}
	},
	
	onButtonB:function(){
		if(this.rectNode!=null){
			this.battleing = 0;
			this.beatMonsterIndex = -1;
			this.closeRect();
		}else if(this.rectNode2!=null){
			this.closeRect2();
		}
		
	},
	
	closeWindow:function(windowName){
		if(this[windowName]!=null){
			this.player.removeChild(this[windowName]);
			this[windowName] = null;
		}
	},
	
	closeRect:function(){
		this.closeWindow("rectNode");
		this.hideAd();
	},
	
	closeRect2:function(){
		this.battleing = 0;
		this.beatMonsterIndex = -1;
		this.closeWindow("rectNode2");
		this.hideAd();
	},
	
	resetAnimation:function(){
		
		this.player.getComponent(cc.Animation).stop(this.playName);
		
		this.playName="none";
	},
	
	beatOneMonstor:function(){
		
		var npc = this.npcArr[this.beatMonsterIndex];
		
		var npcPosition = npc.node.getPosition();
		var mapPosition = this.node.getPosition();
		this.fileNode.setPosition(cc.v2(mapPosition.x+npcPosition.x,mapPosition.y+npcPosition.y));
		this.fireShow(cc.v2(mapPosition.x+npcPosition.x,mapPosition.y+npcPosition.y));

		if(npc.weak&&this[npc.weak]){
			npc.mhp -= this.calDamageOnce(this.attack,0);
		}else{
			npc.mhp -= this.calDamageOnce(this.attack,npc.mde);
		}
		
		cc.audioEngine.play(this.audioSword, false, 1);
		
		if(npc.mhp<=0){//npc血量小于0战斗结束
		
		
			if(npc.gold){//npc掉落金币
				
				var npcx = npc.x,npcy=npc.y;
				
				this.Config.chapterConfig[this.chapterNmb].item.push({x:npcx,y:npcy,t:90,value:npc.gold});
				
				this.createItem(npcx,npcy,90,npc.gold);
				
			}
		
			this.fireDiappear();
			
			this.npcArr.splice(this.beatMonsterIndex,1);
			
			this.Config.chapterConfig[this.chapterNmb].npc.splice(this.beatMonsterIndex,1);
			
			this.node.removeChild(npc.node);
			
			this.battleing = 0;
			
			this.beatMonsterIndex = -1;
			
		}else{//轮到npc攻击玩家
			
			this.node.runAction(cc.sequence(cc.delayTime(0.1),this.beatPlayerCallback ));
		}
		
		
	},
	
	beatPlayer:function(){
		
		console.log("npc攻击玩家");
	
		var npc = this.npcArr[this.beatMonsterIndex];
			
		this.fireShow(this.player.getPosition());
		
		this.hp -= this.calDamageOnce(npc.mac,this.defends);
		
		cc.audioEngine.play(this.audioSword, false, 1);
		
		if(this.hp<=0){
			
			this.fireDiappear();
			
			this.player.runAction(cc.fadeOut(0.8));
			
			cc.audioEngine.play(this.audioDie, false, 1);
		
			this.node.runAction(cc.sequence(cc.delayTime(1),cc.callFunc(function () {
				cc.director.loadScene('ending');
			}, this)));
			
			
		}else{//轮到玩家攻击npc
			
			this.node.runAction(cc.sequence(cc.delayTime(0.1),this.beatNpcCallback ));
			
		}
		
	},
	
	//结算单次伤害
	calDamageOnce(ac,de){
		var damage = ac - de;
		return damage>0?damage:0;
	},
	
	//显示击中标记
	fireShow(position){
		this.fileNode.active = true;
		this.fileNode.zIndex = 9999;
		this.fileNode.setPosition(position);
	},
	
	//击中标记消失
	fireDiappear:function(){
		this.fileNode.runAction(cc.sequence(cc.delayTime(0.1),cc.callFunc(function(){
				this.active = false;
		},this.fileNode)));
	},
	
	beatMonstor:function(x,y){
		
		for(var i=0;i<this.npcArr.length;i++){
			
			var npc = this.npcArr[i];
				
			if(npc.x==x&&npc.y==y){
				
				if(this.calCutHp(npc.mhp,npc.mac,npc.mde,npc.weak)>0){
					this.showRect(i);
				}else{
					this.resetAnimation();
					this.battleing = 1
					this.dir = 0;
					this.beatMonsterIndex = i;
					this.beatOneMonstor();
				}
				
				return false;
					
			}
			
			if(npc.extra){
				
				for(var j=0;j<npc.extra.length;j++){
					
					if(npc.x+npc.extra[j][0]==x&&npc.y+npc.extra[j][1]==y){
				
						if(this.calCutHp(npc.mhp,npc.mac,npc.mde,npc.weak)>0){
							this.showRect(i);
						}else{
							this.resetAnimation();
							this.battleing = 1
							this.dir = 0;
							this.beatMonsterIndex = i;
							this.beatOneMonstor();
						}
				
						return false;
					
					}
					
				}
				
			}
			
		}
		
		return true;
		
	},
	
	beatNpcHelper:function(x,y){
		
		for(var i=0;i<this.npcHelperArr.length;i++){
			
			var npc = this.npcHelperArr[i];
				
			if(npc.x==x&&npc.y==y){
				
				this.showRect2(i);
				
				return false;
					
			}
			
		}
		
		return true;
	},
	
	openDoor:function(x,y){
		
		for(var i=0;i<this.doorArr.length;i++){
			
			var npc = this.doorArr[i];
			
			var doorconf = this.Config.doorConf[npc.t];
			
			if(npc.x == x && npc.y == y){
				
				if(this[doorconf.key]>0){
				
					this[doorconf.key]--;
					
					console.log("doorOpenConf",doorconf.ani);
					
					npc.node.getComponent(cc.Animation).play(doorconf.ani);
					
					npc.node.runAction(cc.sequence(cc.delayTime(1),cc.callFunc(function(){
						this.getParent().removeChild(this);
					},npc.node)));
					
					this.doorArr.splice(i,1);
					
					this.Config.chapterConfig[this.chapterNmb].door.splice(i,1);
					
					cc.audioEngine.play(this.audioDoor, false, 1);
				}
				
				return false;
				
			}
			
		}
		
		return true;
		
		
	},
	
	getItem:function(x,y){
		
		//上下楼
		for(var i=0;i<this.upAndDownArr.length;i++){
			
			var item = this.upAndDownArr[i];
			
			if(item.x==x&&item.y==y){
				
					this.chapterNmb+=item.dir;
				
					this.dir = 0;
				
					this.resetAnimation();
				
					this.initAll();
					
					cc.audioEngine.play(this.audioUpdown, false, 1);
				
				return false;
			}
		}
		
		//传送门
		for(var i=0;i<this.narutoArr.length;i++){
			var item = this.narutoArr[i];
			
			if(item.x==x&&item.y==y){
				
				//this.flyToChapter(item.target);
				
				this.flyToPosAndChapter(item.targetx,item.targety,item.target);
				
				return false;
			}
		}
		
		//吃道具
		for(var i=0;i<this.itemArr.length;i++){
			
			var item = this.itemArr[i];
			
			if(item.x==x&&item.y==y){
				
				var itemconf = this.Config.itemConf[item.t]
				
				var desPosition = null;
				
				if(itemconf.keyNmb){
					this.keyNmb+=itemconf.keyNmb;
					desPosition = this.keyDisplay.node.getParent().getPosition();
				} else if(itemconf.attack){
					this.attack+=itemconf.attack;
					desPosition = this.attackDisplay.node.getParent().getPosition();
				}else if(itemconf.defends){
					this.defends+=itemconf.defends;
					desPosition = this.defendDisplay.node.getParent().getPosition();
				}else if(itemconf.hp){
					this.hp+=itemconf.hp;
					desPosition = this.hpDisplay.node.getParent().getPosition();
				}else if(itemconf.keyNmbR){
					this.keyNmbR+=itemconf.keyNmbR;
					desPosition = this.keyRDisplay.node.getParent().getPosition();
				}else if(itemconf.keyNmbY){
					this.keyNmbY+=itemconf.keyNmbY;
					desPosition = this.keyYDisplay.node.getParent().getPosition();
				}else if(itemconf.iceAble){
					this.iceAble+=itemconf.iceAble;
					desPosition = this.iceDisplay.getParent().getPosition();
					
					this.node.runAction(cc.sequence(cc.delayTime(0.7),cc.callFunc(function(){
						this.iceDisplay.active=true;
					},this)));
					
				}else if(itemconf.tenAble){
					this.tenAble+=itemconf.tenAble;
					desPosition = this.tenDisplay.getParent().getPosition();
					
					this.node.runAction(cc.sequence(cc.delayTime(0.7),cc.callFunc(function(){
						this.tenDisplay.active=true;
					},this)));
					
				}else if(itemconf.godAble){
					this.godAble+=itemconf.godAble;
					desPosition = this.godDisplay.getParent().getPosition();
					this.node.runAction(cc.sequence(cc.delayTime(0.7),cc.callFunc(function(){
						this.godDisplay.active=true;
					},this)));
				}else if(itemconf.gold){
					
					this.goldNmb += item.value;
					desPosition = this.goldDisplay.node.getParent().getPosition();
				}else if(itemconf.flyAble){
					this.flyAble += itemconf.flyAble;
					
					var pos1 = this.flyDisplay.getPosition();
					var pos2 = this.flyTDisplay.getParent().getPosition();
					desPosition = cc.v2(pos2.x+pos1.x,pos2.y+pos1.y);
					
					desPosition = pos2;
					
					this.node.runAction(cc.sequence(cc.delayTime(0.7),cc.callFunc(function(){
						this.flyDisplay.active=true;
					},this)));
				}else if(itemconf.flyTNmb){
					this.flyTNmb += itemconf.flyTNmb;
					var pos1 = this.flyTDisplay.getPosition();
					var pos2 = this.flyTDisplay.getParent().getPosition();
					desPosition = cc.v2(pos2.x+pos1.x,pos2.y+pos1.y);
					
					desPosition = pos2;
					
					
					this.node.runAction(cc.sequence(cc.delayTime(0.7),cc.callFunc(function(){
						this.flyTDisplay.active=true;
					},this)));
					
				}else if(itemconf.flyUNmb){
					this.flyUNmb += itemconf.flyUNmb;
					var pos1 = this.flyUDisplay.getPosition();
					var pos2 = this.flyTDisplay.getParent().getPosition();
					desPosition = cc.v2(pos2.x+pos1.x,pos2.y+pos1.y);
					
					desPosition = pos2;
					
					this.node.runAction(cc.sequence(cc.delayTime(0.7),cc.callFunc(function(){
						this.flyUDisplay.active=true;
					},this)));
				}else if(itemconf.flyDNmb){
					this.flyDNmb += itemconf.flyDNmb;
					var pos1 = this.flyDDisplay.getPosition();
					var pos2 = this.flyDDisplay.getParent().getPosition();
					//desPosition = cc.v2(pos2.x+pos1.x,pos2.y+pos1.y);
					//console.log(desPosition);
					
					desPosition = pos2;
					
					this.node.runAction(cc.sequence(cc.delayTime(0.7),cc.callFunc(function(){
						this.flyDDisplay.active=true;
					},this)));
					
				}else if(itemconf.boomNmb){
					this.boomNmb += itemconf.boomNmb;
					var pos1 = this.boomDisplay.getPosition();
					var pos2 = this.boomDisplay.getParent().getPosition();
					//desPosition = cc.v2(pos2.x+pos1.x,pos2.y+pos1.y);
					desPosition = pos2;
					//console.log(desPosition);
					
					this.node.runAction(cc.sequence(cc.delayTime(0.7),cc.callFunc(function(){
						this.boomDisplay.active=true;
					},this)));
				}
				
				if(desPosition!=null)
					this.itemMove(item,desPosition);
				
				this.itemArr.splice(i,1);
				
				this.Config.chapterConfig[this.chapterNmb].item.splice(i,1);
				
				cc.audioEngine.play(this.audioItem, false, 1);
				
				return true;
			}
		}
		
		return true;
	},
	
	itemMove:function(item,position){
		
		var mapPosition = this.node.getPosition();
		var itemPosition = item.node.getPosition();
		var realPosition = cc.v2(mapPosition.x+itemPosition.x,mapPosition.y+itemPosition.y);
		this.node.removeChild(item.node);
		this.node.getParent().addChild(item.node);
		
		//item.node.setPosition(position);
		
		
		item.node.setPosition(realPosition);
		item.node.setScale(0.8);
		
		item.node.runAction(cc.sequence(cc.jumpBy(0.2, 0, 0, 50, 1),cc.moveTo(0.5,position),cc.callFunc(function(){
				this.getParent().removeChild(this);
		},item.node)));
	},
	
	canMove:function(x,y,dir){
		
		var titlepos = cc.v2(x,y);
		
		if(x>=0&&x<this.maxW&&y>=0&&y<this.maxH&&this.wall.getTileGIDAt(titlepos)==0){
			
			if(!this.firewall){
				return true;
			}else{
				return this.iceAble>0||this.firewall.getTileGIDAt(titlepos)==0;
			}
		}else{
			return false;
		}
	},
	
	mapMove:function(){
		
		var dir = this.dir;
		
		var tempMoveConf = this.moveconf[dir];
		
		if(tempMoveConf!=null){
			
			if(this.playName=="none"){
				this.playName=tempMoveConf.playName;
				this.player.getComponent(cc.Animation).play(this.playName);
			}
			
			var tmpx = this.playerPx+tempMoveConf.x;
			var tmpy = this.playerPy+tempMoveConf.y;
			
			if(this.canMove(tmpx,tmpy,dir)){
				
				if(this.openDoor(tmpx,tmpy)){
					
					if(this.beatMonstor(tmpx,tmpy)){
						
						if(this.beatNpcHelper(tmpx,tmpy)){
				
							this.playerPx+=tempMoveConf.x;
							this.playerPy+=tempMoveConf.y;
						
							this.node.runAction(cc.sequence(cc.moveBy(this.moveOnceTime,-tempMoveConf.x*this.cellSize,tempMoveConf.y*this.cellSize),this.mapMoveCallBack));
						
							return ;
						}
				
					}
				}else{
					this.resetAnimation();
				}
				
			}
			
		}
		
		this.moving = 0;
		
		
	},
	
	onButtonDownDir:function(dir){
		
		if(this.battleing==1||this.dir==dir)return;
		console.log(dir);
		if(dir == 37)
			this.player.getComponent(cc.Sprite).spriteFrame = this.playerFrameL;
		else if(dir == 38)
			this.player.getComponent(cc.Sprite).spriteFrame = this.playerFrameU;
		else if(dir == 39)
			this.player.getComponent(cc.Sprite).spriteFrame = this.playerFrameR;
		else if(dir == 40)
			this.player.getComponent(cc.Sprite).spriteFrame = this.playerFrameD;
		
		this.resetAnimation();
		this.dir=dir;
		
		
		if(!this.moving){
			this.moving = 1;
			this.mapMove();
		}
	},
	
	onMouseUp:function(event){
		
		if(this.battleing==1)return;
		
		this.dir=0;
		
		this.resetAnimation();
		
	},

});
